package com.example.date;

import org.junit.Test;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

/**
 * @author readpage
 * @create 2022-11-07 8:45
 */
public class DurationTest {

    // Duraction表示：时间的区间，用来度量秒和纳秒之间的时间值
    // Duration,可以用于计算LocalDateTime两个时间对象相差的天数、小时数、分数、秒数、纳秒数;
    @Test
    public void test() {
        Duration fromDays = Duration.ofDays(1);
        System.out.println(fromDays.toHours());
        System.out.println(fromDays.toMinutes());
        System.out.println(fromDays.getSeconds());
        System.out.println("*********************");


        LocalDateTime start = LocalDateTime.of(2025, 11, 11, 10, 10);
        LocalDateTime end = LocalDateTime.of(2026, 12, 11, 10, 12);
        // 1. 得到Duration对象
        Duration duration = Duration.between(start, end);

        // 2. 获取两个时间对象间隔的信息
        System.out.println(duration.toDays());  // 间隔多少天
        System.out.println(duration.toHours()); // 间隔多少小时
        System.out.println(duration.toMinutes()); // 间隔多少分
        System.out.println(duration.getSeconds());  // 间隔多少秒
        System.out.println(duration.toMinutes());  // 间隔多少毫秒
        System.out.println(duration.toNanos()); // 间隔多少纳秒

        System.out.println("----------------");
        // LocalDate两个时间相差天数
        long until = start.until(end, ChronoUnit.DAYS);
        System.out.println(until);
    }
}
